import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class PriceCalculator {
    public static void main(String[] args) {
        String filePath = "products.txt"; 
        double totalPrice = 0;
        int itemCount = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader("D:/编程专属文件夹/haha/src/products.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] items = line.split("，"); 
                for (int i = 1; i < items.length; i += 2) {
                    String priceString = items[i].replace("元每台", "").trim(); 
                    double price = Double.parseDouble(priceString); 
                    totalPrice += price;
                    itemCount++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        double averagePrice = itemCount > 0 ? totalPrice / itemCount : 0;
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("D:/编程专属文件夹/haha/src/products.txt", true))) {
            writer.newLine(); // 换行
            writer.write("平均价格: " + averagePrice + "元");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("平均价格: " + averagePrice + "元");
    }
}